package com.example.pc.mytestapplication.util;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.pc.mytestapplication.R;


/**
 * Created by Lxg on 2017/5/15.
 * 标题工具类
 */

public class TitleBarUtils {
    private static TextView tv_titlebar;
    private static ImageView iv_titlebar_back;
    private static ImageView iv_titlebar_right;
    private static TextView tv_titlebar_right;
    private static RelativeLayout rl_titlebar;
    private static SearchView sv_title;
    //    private static View viewf;
    private static RelativeLayout rl_title_bg;

    /**
     * 设置标题栏显示的内容,并设置是否有返回键
     *
     * @param context
     * @param title
     * @param back
     */
    public static View setTitle(final Context context, String title, boolean back) {
        rl_title_bg = (RelativeLayout) ((Activity) context).findViewById(R.id.rl_title_bg);
        iv_titlebar_back = (ImageView) ((Activity) context).findViewById(R.id.iv_titlebar_back);
        tv_titlebar = (TextView) ((Activity) context).findViewById(R.id.tv_titlebar);
        tv_titlebar.setText(title);
        if (context instanceof Activity) {   //当调用者一个activity
            if (back) {
                iv_titlebar_back.setVisibility(View.VISIBLE);
                iv_titlebar_back.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((Activity) context).finish();
                    }
                });
            } else {
                iv_titlebar_back.setVisibility(View.GONE);
            }
        }
        return rl_title_bg;
    }

    /**
     * 设置标题栏显示的内容,并设置是否有返回键
     * <p>
     * 设置背景颜色的
     *
     * @param context
     * @param title
     * @param back
     */
    public static void setTitleColor(final Context context, String title, boolean back, int colo) {

        if (context instanceof Activity) {
            rl_title_bg = (RelativeLayout) ((Activity) context).findViewById(R.id.rl_title_bg);
            iv_titlebar_back = (ImageView) ((Activity) context).findViewById(R.id.iv_titlebar_back);
            tv_titlebar = (TextView) ((Activity) context).findViewById(R.id.tv_titlebar);
            tv_titlebar.setText(title);
            if (back) { //当调用者一个activity
                iv_titlebar_back.setVisibility(View.VISIBLE);
                iv_titlebar_back.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((Activity) context).finish();

                    }
                });
            } else {
                iv_titlebar_back.setVisibility(View.GONE);
            }
            rl_titlebar = (RelativeLayout) ((Activity) context).findViewById(R.id.rl_title_bg);
            //  rl_titlebar = LayoutInflater.from(context).inflate(R.layout.titlebar, null);

            rl_titlebar.setBackgroundResource(colo);
        }
    }

    /**
     * 设置标题栏显示的内容,并设置是否有返回键
     * 设置右侧有图片的 页面
     *
     * @param context
     * @param title
     * @param back
     */
    public static ImageView setTitleDraw(final Context context, String title, boolean back, int drawabsle) {
        rl_title_bg = (RelativeLayout) ((Activity) context).findViewById(R.id.rl_title_bg);

        iv_titlebar_back = (ImageView) ((Activity) context).findViewById(R.id.iv_titlebar_back);
        tv_titlebar = (TextView) ((Activity) context).findViewById(R.id.tv_titlebar);
        tv_titlebar.setText(title);
        if (back) { //当调用者一个activity
            iv_titlebar_back.setVisibility(View.VISIBLE);
            iv_titlebar_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((Activity) context).finish();
                }
            });
        } else {
            iv_titlebar_back.setVisibility(View.GONE);
        }
        if (drawabsle != -1) {
            iv_titlebar_right = (ImageView) ((Activity) context).findViewById(R.id.iv_titlebar_right);
            iv_titlebar_right.setImageResource(drawabsle);
            iv_titlebar_right.setVisibility(View.VISIBLE);
        }
        return iv_titlebar_right;


    }


    /**
     * 针对Fragment嵌套Fragment的特殊情况的处理
     *
     * @param view
     * @param title
     * @param
     */
    public static void setTitle(final View view, String title) {
        rl_title_bg = (RelativeLayout) view.findViewById(R.id.rl_title_bg);

        tv_titlebar = (TextView) view.findViewById(R.id.tv_titlebar);
        tv_titlebar.setText(title);
        iv_titlebar_back = (ImageView) view.findViewById(R.id.iv_titlebar_back);
        iv_titlebar_back.setVisibility(View.GONE);
    }

    /***
     * @param view
     * @param title
     * @param draw  设置右侧图片
     * @return 返回imageview 的控件
     */
    public static ImageView setTitle(final View view, String title, int draw) {
        rl_title_bg = (RelativeLayout) view.findViewById(R.id.rl_title_bg);

        tv_titlebar = (TextView) view.findViewById(R.id.tv_titlebar);
        tv_titlebar.setText(title);
        iv_titlebar_back = (ImageView) view.findViewById(R.id.iv_titlebar_back);
        iv_titlebar_back.setVisibility(View.GONE);
        iv_titlebar_right = (ImageView) view.findViewById(R.id.iv_titlebar_right);
        if (draw > 1) {
            iv_titlebar_right.setVisibility(View.VISIBLE);
            iv_titlebar_right.setImageResource(draw);
        }
        return iv_titlebar_right;
    }


    /**
     * @param view
     * @param title
     * @param txt   设置右侧的文字休息
     * @return 返回textview 的控件
     */
    public static TextView setTitle(final View view, String title, String txt) {
        rl_title_bg = (RelativeLayout) view.findViewById(R.id.rl_title_bg);

        tv_titlebar = (TextView) view.findViewById(R.id.tv_titlebar);
        tv_titlebar.setText(title);
        iv_titlebar_back = (ImageView) view.findViewById(R.id.iv_titlebar_back);
        iv_titlebar_back.setVisibility(View.GONE);

        tv_titlebar_right = (TextView) view.findViewById(R.id.tv_titlebar_right);
        if (!TextUtils.isEmpty(txt)) {
            tv_titlebar_right.setText(txt);
        }

        return tv_titlebar_right;
    }

    /**
     * @param title
     * @param txt   设置右侧的文字休息
     * @return 返回textview 的控件
     */
    public static TextView setTitle(final Context context, String title, String txt) {
        rl_title_bg = (RelativeLayout) ((Activity) context).findViewById(R.id.rl_title_bg);

        tv_titlebar = (TextView) ((Activity) context).findViewById(R.id.tv_titlebar);
        tv_titlebar.setText(title);
        iv_titlebar_back = (ImageView) ((Activity) context).findViewById(R.id.iv_titlebar_back);
        iv_titlebar_back.setVisibility(View.VISIBLE);
        iv_titlebar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) context).finish();
            }
        });
        tv_titlebar_right = (TextView) ((Activity) context).findViewById(R.id.tv_titlebar_right);
        if (!TextUtils.isEmpty(txt)) {
            tv_titlebar_right.setText(txt);
        }

        return tv_titlebar_right;
    }

    /**
     * 带搜索框的标题栏(Fragment)
     *
     * @param view
     * @param title
     * @param issearch
     * @return
     */
    public static SearchView setTitleSearch(View view, String title, boolean issearch, boolean back) {
        rl_title_bg = (RelativeLayout) view.findViewById(R.id.rl_title_bg);

        sv_title = (SearchView) view.findViewById(R.id.sv_title);
        tv_titlebar = (TextView) view.findViewById(R.id.tv_titlebar);
        iv_titlebar_back = (ImageView) view.findViewById(R.id.iv_titlebar_back);
        tv_titlebar.setText(title);
        if (!back) {
            iv_titlebar_back.setVisibility(View.GONE);
        }
        if (issearch) {
            sv_title.setVisibility(View.VISIBLE);
        }
        return sv_title;
    }

}
